<?php

class WorkExperiencesController extends AppController {

    var $helpers = array('Html', 'Form', 'Javascript', 'TinyMce');
    var $components = array('RequestHandler');
    var $name = 'WorkExperiences';

    var $uses = array('WorkExperience', 'Region', 'District', 'Town'); // specifico i modelli che utilizza il controller

    function isAuthorized() {
        return true;
    }
    
    function index($person_id='', $from = '') {
        // se richiamato da countbox ritorno solo il numero di record
         if (isset($this->params['requested'])) {
            return $this->WorkExperience->find('count');
         }
         else {
            $params = array(
                        'conditions' => array('WorkExperience.person_id' => $person_id)
                    ); 
            $this->set('workExperiences', $this->WorkExperience->find('all', $params)); // view variable
            $this->set('person', $this->WorkExperience->Person->findById($person_id));
            $this->set('from', $from);
         }
    }

    function add($person_id='0') {

        if(!empty($this->data) && $this->WorkExperience->validates()){

            if($this->WorkExperience->save($this->data)) {
                $this->Session->setFlash('L\'esperienza di lavoro &egrave; stata salvata');
                $this->redirect(array('action' => 'index', $this->data['WorkExperience']['person_id']));
            }
        }
        else {
            // preparo array delle opzioni per le select del form
            $this->set('companies', $this->WorkExperience->Company->find('list'));
            $this->set('sectors', $this->WorkExperience->Sector->find('list'));
            $this->set('positions', $this->WorkExperience->Position->find('list'));
            $this->set('regions', $this->Region->find('list'));
            $this->set('person_id', $person_id);

        }
            

    }

    function edit($id) {
        // if $_POST is empty read the information from DB
         if(empty($this->data)) {
            $this->WorkExperience->id = $id;
            $this->data = $this->WorkExperience->read();
            $this->set('companies', $this->WorkExperience->Company->find('list'));
            $this->set('sectors', $this->WorkExperience->Sector->find('list'));
            $this->set('positions', $this->WorkExperience->Position->find('list'));
         }
         // if $_POST != empty save the information
         else {
             if($this->WorkExperience->save($this->data)) {
                 $this->Session->setFlash("L'esperienza di istruzione/lavoro è stata aggiornata");
                 $this->redirect(array('action' => 'index', $this->data['WorkExperience']['person_id']));
             }
         }
    }

    /*
     * Delete a work experience of a the Person with $person_id 
     * 
     * @param int $id Work experience id
     * @param int $person_id Person id of the deleted work experience. Used for the redirect
     */
    function delete($id, $person_id) {
        $this->WorkExperience->delete($id);
        $this->Session->setFlash('L\'esperienza è stata eliminata');
        $this->redirect(array('action' => 'index', $person_id));
    }
    
    /*
     * Add a new company to the companies table. AJAX call.
     * 
     * @return string New company id
     */
    function add_company() {
        
        $this->autoRender = false;
        
        if($this->RequestHandler->isAjax()) {
            $this->WorkExperience->Company->save($this->params['form']);
            echo $this->WorkExperience->Company->id;
        }
        
    }

    
    /*
     * Add a new sector to the sectors table. AJAX call.
     * 
     * @return string New sector id
     */
    function add_sector() {
        
        $this->autoRender = false;
        
        if($this->RequestHandler->isAjax()) {
            $this->WorkExperience->Sector->save($this->params['form']);
            echo $this->WorkExperience->Sector->id;
        }
        
    }
    
    /*
     * Add a new position to the positions table. AJAX call.
     * 
     * @return string New position id
     */
    function add_position() {
        
        $this->autoRender = false;
        
        if($this->RequestHandler->isAjax()) {
            $this->WorkExperience->Position->save($this->params['form']);
            echo $this->WorkExperience->Position->id;
        }
        
    }

}

?>
